<?php

require_once(ROOT . '/misc/functions.inc');                    # BASIC SYSTEM FUNCTIONS
require_once(ROOT . '/misc/signs.class.inc');
require_once(ROOT . '/misc/interfaces.inc');
require_once(ROOT . LIBRARY . '/smarty/meccano.smarty.class.inc'); # Smarty templates


# INIT ERROR HENDLER
$ErrorHandler = error::getInstance();

# MAKE FILTERS OBJECT
$FILTER       = new filters();

# START SESSION
session::startSession((!empty($_POST['meccanoSession']) ? $_POST['meccanoSession'] : NULL));

# CONNECT TO DB
$DB = database::getInstance(DB_HOST, DB_NAME, DB_USER, DB_PASS);
$DB->sql = "SET NAMES utf8";
$DB->DB_Query();

# PREPARE REQUEST OBJECT
$REQUEST = request::getInstance();

$FILTER->populate_FILES_to_POST(); // TODO: delete and update as deprecated

$KERNEL  = kernel::getInstance();
$TREE    = tree::getInstance();

$SMARTY = new MeccanoSmarty($REQUEST->getRequestPath());
if(MODE_DEBUG && isset($_GET['_debug']))
{
   $SMARTY->debugging = true;
   $SMARTY->assign('debugging_variable', $_GET['_debug']);
}


/**
 * Autoload Classes
 * @param string $class
 */
function __autoload($class)
{
   $class = '/'. str_replace('_', '/', strtolower($class)) .'.class.inc';
   if(file_exists(ROOT . LIBRARY . $class))
   {
      require_once(ROOT . LIBRARY . $class);
   }
}